<template>
  <a-modal title="生成电子合同" v-model:visible="visible" @close="handleClose" :on-before-ok="handleOk" unmount-on-close>
    <a-radio-group direction="vertical" v-model="contractType">
      <a-radio value="free">免费电子合同</a-radio>
      <a-radio value="pay">收费电子合同</a-radio>
    </a-radio-group>
  </a-modal>
</template>
<script setup>
  import { ref } from 'vue'
  import { Message } from '@arco-design/web-vue'
  import useUserStore from '@/store/modules/user'
  import useNavigate from '@/hooks/useNavigate'
  const { navigateTo } = useNavigate()

  // 选中的合同类型
  const contractType = ref('')
  // 弹窗是否可见
  const visible = ref(false)
  // 读取商户配置开始
  const userStore = useUserStore()
  // 订单信息
  const record = ref(null)
  // 订单类型
  const recordType = ref(null)
  // 显示
  const show = (info, orderType) => {
    record.value = info
    recordType.value = orderType
    // 工单
    if (orderType == 'workOrder') {
      const orderInfo = info.orderInfo
      // 工单对应的订单类型（短租|长租）
      orderType = orderInfo.orderType == 1 ? 'shortOrder' : 'longOrder'
    }
    // 判断是否有付费版的权限
    let hasPayEcontractPermission = false
    // 开通了付费版权限
    if (userStore.syncInventory?.userEcontractEnabled === 1) {
      // 支持的服务类型包含短租
      if (orderType == 'shortOrder' && userStore.syncInventory.userContractServiceType.includes(0)) {
        hasPayEcontractPermission = true
      }
      // 支持的服务类型包含长租
      if (orderType == 'longOrder' && userStore.syncInventory.userContractServiceType.includes(1)) {
        hasPayEcontractPermission = true
      }
    }
    // 有付费版合同权限
    if (hasPayEcontractPermission) {
      // 开启弹窗，选择免费版 or 付费版
      visible.value = true
    } else {
      // 跳转到免费版
      openDetail('free')
    }
  }

  // 关闭
  const handleClose = () => {
    contractType.value = ''
  }

  // 弹窗点击确认
  const handleOk = (done) => {
    if (contractType.value) {
      // 跳转合同详情
      openDetail(contractType.value)
      done()
    } else {
      Message.warning('请选择合同类型')
      done(false)
    }
  }

  // 打开详情
  const openDetail = (contractType) => {
    // 合同记录-tab 普通版 | 付费版
    const contractTabId = contractType == 'free' ? 81 : 82
    // 存储是否自动触发电子合同生成
    sessionStorage.setItem('triggerNewContract', contractTabId)
    // 工单
    if (recordType.value == 'workOrder') {
      navigateTo(
        '/work-order/detail/' +
          record.value.id +
          '?tab=3&associationorderTab=4&orderId=' +
          record.value.refId +
          '&workCode=' +
          record.value.workCode +
          '&orderType=' +
          record.value.orderInfo.orderType +
          '&status=' +
          record.value.status +
          '&contractTab=' +
          contractTabId,
        `${record.value.workCode}-详情`
      )
    } else {
      if (recordType.value == 'shortOrder') {
        navigateTo(`/short-order/detail/${record.value.id}?tab=8&contractTab=${contractTabId}`, `${record.value.orderCode}-短租详情`)
      } else {
        navigateTo(`/long-order/detail/${record.value.id}?tab=10&contractTab=${contractTabId}`, `${record.value.orderNo}-长租详情`)
      }
    }
  }

  defineExpose({
    show,
  })
</script>
<style lang="less" scoped></style>
